package org.molamil.acto.types {	
	import org.molamil.acto.core.parsers.ParserError;	
	
	/**
	 * Type parser expecting the value passed when <code>parse()</code> to 
	 * be "int compatible". These values can e.g. be "100", "-12", 
	 * "0xFF0000", etc.
	 * 
	 * <p>Note that unlike the <code>int()</code> casting <code>parse()</code> 
	 * will fail if value is empty or if there are decimals.</p>
	 * 
	 * @example The following code will parse "28" into <code>28</code>
	 * and will trace "HOLLY COW, I parsed an int!!!":
	 * <listing version="3.0" >
	 * var parser:AbstractTypeParser = new IntTypeParser();
	 * var n:int = parser.parse("28");
	 * if (n == 28)
	 *    trace("HOLLY COW, I parsed an int!!!");
	 * </listing>
	 * 
	 * @author Jorge Hernandez
	 */
	public class IntTypeParser extends AbstractTypeParser {
		
		
		// CONSTANTS
		
		/**
		 * @inheritDoc
		 */
		public static const PRIORITY:uint = 300;
		
		/**
		 * @inheritDoc
		 */
		public static const TYPE:Class = int;
		
		
		// PROTECTED METHODS
		
		/**
		 * Returns the integer representation of the passed value, or throws
		 * an <code>Error</code> if value does not contain any representation 
		 * that can be parsed into an <code>int</code>
		 * 
		 * <p>Note that unlike the <code>int()</code> casting this method will 
		 * fail if value is empty or if there are decimals.</p>
		 * 
		 * <p>The following strings will parse into an int</p>
		 * <ul>
		 * 	<li>"100" will parse into <code>100</code></li>
		 * 	<li>"1" will parse into <code>1</code></li>
		 * 	<li>"-0x100" will parse into <code>-256</code></li>
		 * </ul>
		 * 
		 * @param value <code>String</code> value to test
		 * @return Integer representation of the value passed
		 * @throws org.molamil.acto.core.errors.ParserError The value can't
		 * 	be converted to an <code>int</code>
		 */
		override protected function doParse(value:String):* {
			
			if (value == null)
				throw new ParserError("ERROR: value is null and cannot be parsed " +
					"into an int");
			
			var n:int = int(value);
			var nv:Number = Number(value);
			
			if (value == "")
				throw new ParserError("ERROR: '" + value + "' cannot be parsed " +
					"into an int (value is empty)");
					
			if (value.indexOf(".") != -1)
				throw new ParserError("ERROR: '" + value + "' cannot be parsed " +
					"into an int (value contains decimals)");
					
			if (isNaN(nv))
				throw new ParserError("ERROR: '" + value + "' cannot be parsed " +
					"into an int");
					
			if (nv > int.MAX_VALUE)
				throw new ParserError("ERROR: '" + value + "' cannot be parsed " +
					"into an int (too big)");
					
			if (nv < int.MIN_VALUE)
				throw new ParserError("ERROR: '" + value + "' cannot be parsed " +
					"into an int (too small)");
			
			if (n == 0 && nv != 0)
				throw new ParserError("ERROR: '" + value + "' cannot be parsed " +
					"into an int");
			
			return n;
			
		}
		
		
	}
	
}